//
//  UIView+Extension.m
//  MJRefreshExample
//
//  Created by MJ Lee on 14-5-28.
//  Copyright (c) 2014年 itcast. All rights reserved.
//

#import "UIView+Extension.h"
#define THSystemWhiteColor [UIColor whiteColor]

@implementation UIView (Extension)

- (void)setX:(CGFloat)x
{
    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
}

- (CGFloat)x
{
    return self.frame.origin.x;
}

- (void)setY:(CGFloat)y
{
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}

- (CGFloat)y
{
    return self.frame.origin.y;
}

-(void)setMaxX:(CGFloat)maxX
{
    self.x = maxX - self.width;
}

- (CGFloat)maxX
{
    return CGRectGetMaxX(self.frame);
}

- (void)setMaxY:(CGFloat)maxY
{
    self.y = maxY - self.height;
}

- (CGFloat)maxY
{
    return CGRectGetMaxY(self.frame);
}

- (void)setWidth:(CGFloat)width
{
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
}

- (CGFloat)width
{
    return self.frame.size.width;
}

- (void)setHeight:(CGFloat)height
{
    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
}

- (CGFloat)height
{
    return self.frame.size.height;
}
- (CGFloat)top
{
    return self.frame.origin.y;
}

- (void)setTop: (CGFloat) newtop
{
    CGRect newframe = self.frame;
    newframe.origin.y = newtop;
    self.frame = newframe;
}

- (CGFloat)left
{
    return self.frame.origin.x;
}

- (void)setLeft: (CGFloat) newleft
{
    CGRect newframe = self.frame;
    newframe.origin.x = newleft;
    self.frame = newframe;
}

- (CGFloat)bottom
{
    return self.frame.origin.y + self.frame.size.height;
}

- (void)setBottom: (CGFloat) newbottom
{
    CGRect newframe = self.frame;
    newframe.origin.y = newbottom - self.frame.size.height;
    self.frame = newframe;
}

- (CGFloat)right
{
    return self.frame.origin.x + self.frame.size.width;
}

- (void)setRight: (CGFloat) newright
{
    CGFloat delta = newright - (self.frame.origin.x + self.frame.size.width);
    CGRect newframe = self.frame;
    newframe.origin.x += delta ;
    self.frame = newframe;
}


- (void)setSize:(CGSize)size
{
    CGRect frame = self.frame;
    frame.size = size;
    self.frame = frame;
}

- (CGSize)size
{
    return self.frame.size;
}

- (void)setOrigin:(CGPoint)origin
{
    CGRect frame = self.frame;
    frame.origin = origin;
    self.frame = frame;
}

- (CGPoint)origin
{
    return self.frame.origin;
}
-(void) setCorner:(float) r
{
    if (r < 0 )
    {
        r = 0 ;
    }
    self.layer.cornerRadius = r ;
    self.layer.masksToBounds = YES ;
}
- (CGFloat)centerX {
    return self.center.x;
}

- (void)setCenterX:(CGFloat)centerX {
    self.center = CGPointMake(centerX, self.center.y);
}

- (CGFloat)centerY {
    return self.center.y;
}

- (void)setCenterY:(CGFloat)centerY {
    self.center = CGPointMake(self.center.x, centerY);
}
- (void)removeAllSubviews {
    for (UIView *subview in self.subviews) {
        if ([subview isKindOfClass:[UIView class]]) {
            [subview removeFromSuperview];
        }
        
    }
}

- (void)removeAllGestureRecognizers {
    for (UIGestureRecognizer *gestureRecognizer in self.gestureRecognizers) {
        [self removeGestureRecognizer:gestureRecognizer];
    }
}

- (UIImage *)screenshotWithQuality:(CGFloat)imageQuality {
    UIGraphicsBeginImageContext(self.bounds.size);
    if([self respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]){
        [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:NO];
    }else{
        [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    }
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSData *imageData = UIImageJPEGRepresentation(image, imageQuality>0?imageQuality:0.75f);
    image = [UIImage imageWithData:imageData];
    return image;
}

+ (UIView *)createLineView:(UIView *)superView
{
    return [self createLineView:superView frame:CGRectMake(0, superView.height - 0.5, kScreenWidth, 0.5)];
}

+ (UIView *)createLineViewInTop:(UIView *)superView
{
    return [self createLineView:superView frame:CGRectMake(0, 0, kScreenWidth, 0.5)];
}

+ (UIView *)createLineViewInbottom:(UIView *)superView
{
    return [self createLineView:superView frame:CGRectMake(0, superView.height - 0.5, superView.width, 1)];
}

+ (UIView *)createLineView:(UIView *)superView frame:(CGRect)rect
{
    UIView *line = [[UIView alloc]initWithFrame:rect];
    line.backgroundColor = THWhiteAlplaColor(0.36);
    [superView addSubview:line];
    return line;
}

// Query other frame locations
- (CGPoint)bottomRight
{
    CGFloat x = self.frame.origin.x + self.frame.size.width;
    CGFloat y = self.frame.origin.y + self.frame.size.height;
    return CGPointMake(x, y);
}

- (CGPoint)bottomLeft
{
    CGFloat x = self.frame.origin.x;
    CGFloat y = self.frame.origin.y + self.frame.size.height;
    return CGPointMake(x, y);
}


- (CGPoint)topRight
{
    CGFloat x = self.frame.origin.x + self.frame.size.width;
    CGFloat y = self.frame.origin.y;
    return CGPointMake(x, y);
}



+ (void)createTapGestureInView:(UIView *)view target:(id)target action:(SEL)selector
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:target action:selector];
    [view addGestureRecognizer:tap];
}

- (void)createShadowWithColor:(UIColor *)color offSet:(CGSize)offset opacity:(CGFloat)opacity radius:(CGFloat)radius
{
    [self createShadowWithColor:color offSet:offset opacity:opacity radius:radius cornerRadius:5.];
}

- (void)createShadowWithColor:(UIColor *)color offSet:(CGSize)offset opacity:(CGFloat)opacity radius:(CGFloat)radius cornerRadius:(CGFloat)conerRadius
{
    self.layer.masksToBounds = NO;
    self.layer.cornerRadius = conerRadius; // if you like rounded corners
    self.layer.shadowColor = color.CGColor;//阴影颜色
    self.layer.shadowOffset = offset;//偏移距离
    self.layer.shadowOpacity = opacity;//不透明度
    self.layer.shadowRadius = radius;//半径
}

- (void)drawLineFromPoint:(CGPoint)fromPoint toPoint:(CGPoint)toPoint lineColor:(UIColor *)lineColor lineWidth:(CGFloat)width
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextBeginPath(context);
    
    CGFloat lineWidth = width;
    
    CGContextSetLineWidth(context,lineWidth);//线宽度
    
    CGColorRef lineColorRef = lineColor.CGColor;
    CGContextSetStrokeColorWithColor(context,lineColorRef);
    
    CGContextSetFillColorWithColor(context, THSystemWhiteColor.CGColor);
    
    CGContextDrawPath(context, kCGPathFill);
    
    CGContextFillPath(context);
    
    CGContextMoveToPoint(context, fromPoint.x, fromPoint.y);
    
    CGContextAddLineToPoint(context, toPoint.x, toPoint.y);
    CGContextDrawPath(context, kCGPathEOFillStroke);
}


@end
